{
 "cells": [
  {
   "cell_type": "markdown",
   "source": [
    "# Field\n",
    "Field 是 Python 中用于定义数据模型字段的工具，通常与 pydantic 库一起使用。pydantic 是一个用于数据验证和解析的库，广泛应用于 FastAPI 等框架中。Field 用于为数据模型中的字段提供额外的配置和元数据，例如默认值、验证规则、描述等。\n",
    "## 基本用法"
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "de8dcd6620e1c760"
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "ename": "ValidationError",
     "evalue": "1 validation error for User\nage\n  Input should be less than 150 [type=less_than, input_value=250, input_type=int]\n    For further information visit https://errors.pydantic.dev/2.9/v/less_than",
     "output_type": "error",
     "traceback": [
      "\u001B[0;31m---------------------------------------------------------------------------\u001B[0m",
      "\u001B[0;31mValidationError\u001B[0m                           Traceback (most recent call last)",
      "Cell \u001B[0;32mIn[3], line 8\u001B[0m\n\u001B[1;32m      5\u001B[0m     name: \u001B[38;5;28mstr\u001B[39m \u001B[38;5;241m=\u001B[39m Field(\u001B[38;5;241m.\u001B[39m\u001B[38;5;241m.\u001B[39m\u001B[38;5;241m.\u001B[39m, description\u001B[38;5;241m=\u001B[39m\u001B[38;5;124m\"\u001B[39m\u001B[38;5;124mUser name\u001B[39m\u001B[38;5;124m\"\u001B[39m, min_length\u001B[38;5;241m=\u001B[39m\u001B[38;5;241m1\u001B[39m, max_length\u001B[38;5;241m=\u001B[39m\u001B[38;5;241m100\u001B[39m)\n\u001B[1;32m      6\u001B[0m     age: \u001B[38;5;28mint\u001B[39m \u001B[38;5;241m=\u001B[39m Field(\u001B[38;5;241m18\u001B[39m, description\u001B[38;5;241m=\u001B[39m\u001B[38;5;124m\"\u001B[39m\u001B[38;5;124mUser age\u001B[39m\u001B[38;5;124m\"\u001B[39m, gt\u001B[38;5;241m=\u001B[39m\u001B[38;5;241m0\u001B[39m, lt\u001B[38;5;241m=\u001B[39m\u001B[38;5;241m150\u001B[39m)\n\u001B[0;32m----> 8\u001B[0m user \u001B[38;5;241m=\u001B[39m User(\u001B[38;5;28mid\u001B[39m\u001B[38;5;241m=\u001B[39m\u001B[38;5;241m1\u001B[39m, name\u001B[38;5;241m=\u001B[39m\u001B[38;5;124m\"\u001B[39m\u001B[38;5;124mAlice\u001B[39m\u001B[38;5;124m\"\u001B[39m, age\u001B[38;5;241m=\u001B[39m\u001B[38;5;241m250\u001B[39m)\n\u001B[1;32m      9\u001B[0m \u001B[38;5;28mprint\u001B[39m(user)\n",
      "File \u001B[0;32m/opt/anaconda3/envs/ai_312/lib/python3.12/site-packages/pydantic/main.py:212\u001B[0m, in \u001B[0;36mBaseModel.__init__\u001B[0;34m(self, **data)\u001B[0m\n\u001B[1;32m    210\u001B[0m \u001B[38;5;66;03m# `__tracebackhide__` tells pytest and some other tools to omit this function from tracebacks\u001B[39;00m\n\u001B[1;32m    211\u001B[0m __tracebackhide__ \u001B[38;5;241m=\u001B[39m \u001B[38;5;28;01mTrue\u001B[39;00m\n\u001B[0;32m--> 212\u001B[0m validated_self \u001B[38;5;241m=\u001B[39m \u001B[38;5;28mself\u001B[39m\u001B[38;5;241m.\u001B[39m__pydantic_validator__\u001B[38;5;241m.\u001B[39mvalidate_python(data, self_instance\u001B[38;5;241m=\u001B[39m\u001B[38;5;28mself\u001B[39m)\n\u001B[1;32m    213\u001B[0m \u001B[38;5;28;01mif\u001B[39;00m \u001B[38;5;28mself\u001B[39m \u001B[38;5;129;01mis\u001B[39;00m \u001B[38;5;129;01mnot\u001B[39;00m validated_self:\n\u001B[1;32m    214\u001B[0m     warnings\u001B[38;5;241m.\u001B[39mwarn(\n\u001B[1;32m    215\u001B[0m         \u001B[38;5;124m'\u001B[39m\u001B[38;5;124mA custom validator is returning a value other than `self`.\u001B[39m\u001B[38;5;130;01m\\n\u001B[39;00m\u001B[38;5;124m'\u001B[39m\n\u001B[1;32m    216\u001B[0m         \u001B[38;5;124m\"\u001B[39m\u001B[38;5;124mReturning anything other than `self` from a top level model validator isn\u001B[39m\u001B[38;5;124m'\u001B[39m\u001B[38;5;124mt supported when validating via `__init__`.\u001B[39m\u001B[38;5;130;01m\\n\u001B[39;00m\u001B[38;5;124m\"\u001B[39m\n\u001B[1;32m    217\u001B[0m         \u001B[38;5;124m'\u001B[39m\u001B[38;5;124mSee the `model_validator` docs (https://docs.pydantic.dev/latest/concepts/validators/#model-validators) for more details.\u001B[39m\u001B[38;5;124m'\u001B[39m,\n\u001B[1;32m    218\u001B[0m         category\u001B[38;5;241m=\u001B[39m\u001B[38;5;28;01mNone\u001B[39;00m,\n\u001B[1;32m    219\u001B[0m     )\n",
      "\u001B[0;31mValidationError\u001B[0m: 1 validation error for User\nage\n  Input should be less than 150 [type=less_than, input_value=250, input_type=int]\n    For further information visit https://errors.pydantic.dev/2.9/v/less_than"
     ]
    }
   ],
   "source": [
    "from pydantic import BaseModel, Field\n",
    "\n",
    "class User(BaseModel):\n",
    "    id: int = Field(..., description=\"User ID\")\n",
    "    name: str = Field(..., description=\"User name\", min_length=1, max_length=100)\n",
    "    age: int = Field(18, description=\"User age\", gt=0, lt=150)\n",
    "\n",
    "user = User(id=1, name=\"Alice\", age=25)\n",
    "print(user)\n"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-11-01T07:51:30.315222Z",
     "start_time": "2024-11-01T07:51:29.662637Z"
    }
   },
   "id": "340eeeaba3c2f339",
   "execution_count": 3
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
